#!/usr/bin/env sh

FIFO=/tmp/panel_fifo
FONT="*-tewi-medium-*-*--11-*"
WIDTH="1330"
HEIGHT="24"
X="15"
Y="15"

[ -e $FIFO ] && rm $FIFO
mkfifo $FIFO

clock() {
	while true; do
		date +"S%H:%M"
		sleep 2
	done
}

workspaces() {
	while true; do
		local cur=$(xprop -root _NET_CURRENT_DESKTOP | awk '{print $3}')
		local total=${DESKTOP_COUNT}
		local seq=""

		for ((i=0;i<cur;i++)); do
			seq+="${EMPTY}${i}${CLR}"
		done

		seq+="%{U$YLW}%{+u}${BLACK}${cur}${SEP}%{-u}"

		min=$cur+1
		for ((i=min;i<total;i++)); do
			seq+="${EMPTY}${i}${CLR}"
		done

		echo "WORKSPACES ${seq}${CLR}"
		echo "WORKSPACES ${seq}"

		sleep 0.5
	done
}

killall -9 lemonbar 2> /dev/null

clock > $FIFO &
workspaces > $FIFO &

while read -r line; do
	case $line in
		S*)
			ret_time="${line#?}"
			;;
		W*)
			ret_work="${line#WORKSPACES }"
	esac
	printf " %s\n" "%{l}$ret_work%{c}$ret_time"
done < $FIFO | \
lemonbar \
	-f "$FONT" \
	-d \
	-u 2 \
	-n bar \
	-g ${WIDTH}x${HEIGHT}+${X}+${Y} \
	-f "$FONT" \
	-F "#ffffff" \
	-B "#1a1512" \
	| sh
